import { defineStore } from "pinia";
import { computed, reactive, ref } from "vue";
import { HistoryStackItemEnum, HistoryTargetTypeEnum, type ChartHistoryStoreType, type HistoryItemType } from "./types/dataviewHistory";
import { HistoryStackEnum } from "./types/dataviewHistory";
import type { CreateComponentGroupType, CreateComponentType } from "@/views/dataview/packages/public/type";
import { HistoryActionTypeEnum, type EditCanvasType } from "./types";
import { editHistoryMax } from "@/views/dataview/setting/designSetting";

// 大屏历史记录
export const useDataViewHistoryStore = defineStore("dataViewHistory", () => {
    const dataviewHistoryState =reactive<ChartHistoryStoreType>({
        // 后退栈
        backStack: [],
        // 前进栈
        forwardStack: [],
    })
    const getBackStack = computed(()=>dataviewHistoryState[HistoryStackEnum.BACK_STACK])
    const getForwardStack = computed(()=>dataviewHistoryState[HistoryStackEnum.FORWARD_STACK])
    const getBackLen = computed(()=>dataviewHistoryState[HistoryStackEnum.BACK_STACK].length)
    const getForwardLen = computed(()=>dataviewHistoryState[HistoryStackEnum.FORWARD_STACK].length)


    const createStackItem = (
        item:CreateComponentType[] | CreateComponentGroupType[] | EditCanvasType[],
        actionType:HistoryActionTypeEnum,
        targetType:HistoryTargetTypeEnum = HistoryTargetTypeEnum.CHART
    )=>{
        pushBackStackItem(
            Object.freeze({
                [HistoryStackItemEnum.ID]: new Date().getTime().toString(),
                [HistoryStackItemEnum.HISTORY_DATA]: item,
                [HistoryStackItemEnum.ACTION_TYPE]: actionType,
                [HistoryStackItemEnum.TARGET_TYPE]: targetType
            })
        )
    }

    const canvasInit = (canvas:EditCanvasType)=>{
        createStackItem([canvas],HistoryActionTypeEnum.ADD,HistoryTargetTypeEnum.CANVAS)
    }

    // 推入后退栈
    const pushBackStackItem = (item:HistoryItemType | Array<HistoryItemType>,notClear = false)=>{
        if(Array.isArray(item)){
            dataviewHistoryState[HistoryStackEnum.BACK_STACK] = [...dataviewHistoryState[HistoryStackEnum.BACK_STACK],...item]
        }else{
            dataviewHistoryState[HistoryStackEnum.BACK_STACK].push(item)
        }
        // 一旦超出最大值，则清空最先的数据
        dataviewHistoryState[HistoryStackEnum.BACK_STACK].splice(0,dataviewHistoryState[HistoryStackEnum.BACK_STACK].length - editHistoryMax)
        if(notClear) return 
        clearForwardStack()
    }

    // 推入前进栈
    const pushForwardStack = (item: HistoryItemType | Array<HistoryItemType>)=>{
        if(Array.isArray(item)){
            dataviewHistoryState[HistoryStackEnum.FORWARD_STACK] = [...dataviewHistoryState[HistoryStackEnum.FORWARD_STACK],...item]
        }else {
            dataviewHistoryState[HistoryStackEnum.FORWARD_STACK].push(item)
        }
    }

    // * 清空前进栈
    const clearForwardStack = ()=>{
        dataviewHistoryState[HistoryStackEnum.FORWARD_STACK] = []
    }

    const clearBackStack = ()=>{
        dataviewHistoryState[HistoryStackEnum.BACK_STACK] = []
    }

    // 移除后退栈
    const popBackStackItem = ()=>{
        if(getBackLen.value > 0){
            return dataviewHistoryState[HistoryStackEnum.BACK_STACK].pop()
        }
    }
    // 移除前进栈
    const popForwardStack = ()=>{
        if(getForwardLen.value > 0){
            return dataviewHistoryState[HistoryStackEnum.FORWARD_STACK].pop()
        }
    }

    const backAction = ()=>{
        // 排除画布初始化,保留一个数据
        if(getBackLen.value > 1){
            const targetData = popBackStackItem()
            if(!targetData) return
            // 移除记录到前进堆
            pushForwardStack(targetData)
            return targetData
        }
    }

    const forwardAction = ()=>{
        if(getForwardLen.value){
            const targetData = popForwardStack()
            if(!targetData) return 
            pushBackStackItem(targetData,true)
            return targetData
        }
    }

    // 新增组件记录
    const createAddHistory = (item: Array<CreateComponentType | CreateComponentGroupType>)=>{
        createStackItem(item,HistoryActionTypeEnum.ADD,HistoryTargetTypeEnum.CHART)
    }

    const createUpdateHistory = (item: Array<CreateComponentType | CreateComponentGroupType>)=>{
        createStackItem(item,HistoryActionTypeEnum.UPDATE,HistoryTargetTypeEnum.CHART)
    }
        // * 删除组件记录
    const createDeleteHistory = (item: Array<CreateComponentType | CreateComponentGroupType>) => {
        createStackItem(item, HistoryActionTypeEnum.DELETE, HistoryTargetTypeEnum.CHART)
    }
    // * 移动组件记录
    const createMoveHistory = (item: Array<CreateComponentType | CreateComponentGroupType>)=> {
        createStackItem(item, HistoryActionTypeEnum.MOVE, HistoryTargetTypeEnum.CHART)
    }

    const createLayerHistory = (
        item: Array<CreateComponentType | CreateComponentGroupType>,
        type:
        | HistoryActionTypeEnum.TOP
        | HistoryActionTypeEnum.DOWN
        | HistoryActionTypeEnum.UP
        | HistoryActionTypeEnum.BOTTOM
    )=>{
        createStackItem(item, type, HistoryTargetTypeEnum.CHART)
    }

    // 剪切记录
    const createPasteHistory = (item: Array<CreateComponentType | CreateComponentGroupType>)=>{
        createStackItem(item,HistoryActionTypeEnum.CUT,HistoryTargetTypeEnum.CHART)
    }
    const createGroupHistory = (item: Array<CreateComponentType | CreateComponentGroupType>)=>{
        createStackItem(item,HistoryActionTypeEnum.GROUP,HistoryTargetTypeEnum.CHART)
    }

    const createUnGroupHistory = (item: Array<CreateComponentType | CreateComponentGroupType>)=>{
        createStackItem(item,HistoryActionTypeEnum.UN_GROUP,HistoryTargetTypeEnum.CHART)
    }
    return {
        dataviewHistoryState,
        getBackStack,
        getForwardStack,
        getBackLen,
        getForwardLen,
        createMoveHistory,
        canvasInit,
        pushBackStackItem,
        pushForwardStack,
        clearBackStack,
        popBackStackItem,
        popForwardStack,
        backAction,
        forwardAction,
        createAddHistory,
        createUpdateHistory,
        createDeleteHistory,
        createLayerHistory,
        createPasteHistory,
        createGroupHistory,
        createUnGroupHistory
    };
});